package com.meepo.basic

import com.meepo.basic.schema.Session
import com.meepo.basic.schema.common.ResponseWrapper
import com.meepo.sdk.component.observer.Observer
import com.meepo.sdk.component.observer.ObserverManager.applySchedulers
import java.util.*

internal object State {

    private val session = Session(
        sessionId = UUID.randomUUID().toString(),
        settings = Session.AppSettings(
            h5Domain = "http://h5.meepod.cn",
            feedSettings = Session.AppSettings.FeedSettings(maxImageCount = 9)
        )
    )

    fun getSession(): Session {
        return session
    }

    fun initSettings(callback: () -> Unit) {
        API.get().fetchAppSettings()
            .applySchedulers()
            .subscribe(object : Observer<ResponseWrapper<Session.AppSettings>>() {
                override fun onSuccess(t: ResponseWrapper<Session.AppSettings>?) {
                    t?.data?.let { d ->
                        d.feedSettings?.let { session.settings.feedSettings = it }
                        if (d.h5Domain?.isNotEmpty() == true) session.settings.h5Domain = d.h5Domain!!
                    }
                }

                override fun onFailure(e: Throwable) {
                }

                override fun onFinal() {
                    callback()
                }
            })
    }
}